package com.janj.utils;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;


//1.写一个类,实现 DataSourse
//2.准备一个集合对象,存放Connection对象
//3.采用静态代码块,初始化 10个连接的对象
//4.在 getConnection 方法当中,定义判断语句,获取连接
//5.定义 getSize方法,返回集合的长度大小
public class MyDataSource implements DataSource {
     private static List<Connection> array = Collections.synchronizedList(new ArrayList<>());

     static{
         for (int i = 0; i < 10; i++) {
             array.add(JDBCUtils.getConnection());
         }
     }

    @Override
    public Connection getConnection() throws SQLException {
        //当数量低于0的时候,制造异常
         if(array.size() == 0){
            throw new RuntimeException("连接数量不够...");
        }
        //获取连接
        Connection conn = array.remove(0);
        return conn ;
    }

    //定义 getSize的方法
    public int getSize(){
         return array.size();
    }


    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
